package com.platform.common.utils;

import java.io.File;
import java.text.MessageFormat;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

import com.platform.common.utils.log.LoggerFactory;

/**
 * <pre>
 * 功能：系统配置类
 * </pre>
 * 
 * @author wei.gao
 */
public class Configuration {
	private static XMLConfiguration configuration = null;
	
	private static final String RES_XML_NAME = "system.cfg.xml";
	private static final String RES_BASE_NAME = "resources";
	private static final String MSG_BASE_NAME = "i18n/messages";
	public static final String LINE_SEPARATOR = System.getProperty("line.separator");
	public static final String PATH_SEPARATOR = System.getProperty("path.separator");
	/**
	 * 后面不带文件分隔符的地址
	 */
	public static String ROOT;
	public static String CURRENT_PATH;
	public static String LOG_PATH;
	public static String CLASSPATH;
	public static String CONFIG_PATH;
	public static String RESOURCES;
	
	static {
		LoggerFactory.get().debug(Configuration.class, "LINE_SEPARATOR=" + LINE_SEPARATOR);
		LoggerFactory.get().debug(Configuration.class, "PATH_SEPARATOR=" + PATH_SEPARATOR);
		ROOT = new File(Configuration.class.getResource("/").getPath()).getParentFile().getParent().toString();
		LoggerFactory.get().debug(Configuration.class, "ROOT=" + ROOT);
		CURRENT_PATH = new File(Configuration.class.getResource("/").getPath()).toString();
		LoggerFactory.get().debug(Configuration.class, "CURRENT_PATH=" + CURRENT_PATH);
		CONFIG_PATH = StringUtils.concat(ROOT, File.separator, "WEB-INF", File.separator, "conf");
		LoggerFactory.get().debug(Configuration.class, "CONFIG_PATH=" + CONFIG_PATH);
		LOG_PATH = StringUtils.concat(ROOT, File.separator, "WEB-INF", File.separator, "logs");
		LoggerFactory.get().debug(Configuration.class, "LOG_PATH=" + LOG_PATH);
		RESOURCES = StringUtils.concat(ROOT, File.separator, "WEB-INF", File.separator, "classes", File.separator, "files");
		LoggerFactory.get().debug(Configuration.class, "RESOURCES=" + RESOURCES);
		
		StringBuilder sb = new StringBuilder();
		List<File> files = FileUtils.getFiles(new File(StringUtils.concat(ROOT, File.separator, "WEB-INF", File.separator, "lib")));
		sb.append(".");
		for (File file : files) {
			sb.append(PATH_SEPARATOR);
			sb.append(file.getAbsolutePath());
		}
		sb.append(PATH_SEPARATOR);
		sb.append(CURRENT_PATH);
		CLASSPATH = sb.toString();
		LoggerFactory.get().debug(Configuration.class, "CLASSPATH=" + CLASSPATH);
		try {
			configuration = new XMLConfiguration();
			configuration.setFile(new File(StringUtils.concat(CURRENT_PATH, File.separator, RES_XML_NAME)));
			configuration.setReloadingStrategy(new FileChangedReloadingStrategy());
			configuration.load();
			configuration.setEncoding(getEncoding());
		} catch (ConfigurationException e) {
			LoggerFactory.get().error(ExcptionUtils.getStackTraceAsString(e));
		}
	}
	
	/**
	 * <pre>
	 * 功能：获得字符串属性
	 * </pre>
	 * 
	 * @param key
	 * @return
	 */
	public static String get(String key) {
		return configuration.getString(key);
	}
	
	/**
	 * <pre>
	 * 功能：获得字符串属性
	 * </pre>
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static String get(String key, String defaultValue) {
		return configuration.getString(key, defaultValue);
	}
	
	/**
	 * <pre>
	 * 功能：获得字符串属性s
	 * </pre>
	 * 
	 * @param key
	 * @return
	 */
	public static String[] getArray(String key) {
		return configuration.getStringArray(key);
	}
	
	/**
	 * <pre>
	 * 功能：获取编码
	 * </pre>
	 * 
	 * @return
	 */
	public static String getEncoding() {
		return "utf-8";
	}
	
	/**
	 * <pre>
	 * 功能：获得资源信息
	 * </pre>
	 * 
	 * @param key
	 * @param params
	 * @return
	 */
	public static String getResource(String key, Object... params) {
		if (params == null || params.length < 1) {
			return ResourceBundle.getBundle(RES_BASE_NAME).getString(key);
		} else {
			return MessageFormat.format(ResourceBundle.getBundle(RES_BASE_NAME).getString(key), params);
		}
	}
	
	/**
	 * <pre>
	 * 功能：获得资源信息
	 * </pre>
	 * 
	 * @param key
	 * @return
	 */
	public static String getResource(String key) {
		String value = null;
		try {
			ResourceBundle resource = ResourceBundle.getBundle(RES_BASE_NAME);
			String temp = resource.getString(key);
			if (temp != null) {
				value = temp;
			}
		} catch (Exception e) {
		}
		return value;
	}
	
	/**
	 * <pre>
	 * 功能：获得资源信息
	 * </pre>
	 * 
	 * @param key
	 * @param params
	 * @return
	 */
	public static String i18nResource(String key, Object... params) {
		if (params == null || params.length < 1) {
			return ResourceBundle.getBundle(MSG_BASE_NAME).getString(key);
		} else {
			return MessageFormat.format(ResourceBundle.getBundle(MSG_BASE_NAME).getString(key), params);
		}
	}
	
	/**
	 * <pre>
	 * 功能：获得资源信息
	 * </pre>
	 * 
	 * @param key
	 * @return
	 */
	public static String i18nResource(String key) {
		String value = null;
		try {
			ResourceBundle resource = ResourceBundle.getBundle(MSG_BASE_NAME);
			String temp = resource.getString(key);
			if (temp != null) {
				value = temp;
			}
		} catch (Exception e) {
		}
		return value;
	}
}
